﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package org.aswing {
    import flash.events.*;
    import org.aswing.plaf.*;
    import org.aswing.event.*;
    import org.aswing.border.*;
    import org.aswing.plaf.basic.*;

    public class JFrameTitleBar extends Container implements FrameTitleBar, UIResource {

        protected var iconifiedButton:AbstractButton;
        protected var maximizeButton:AbstractButton;
        protected var restoreButton:AbstractButton;
        protected var closeButton:AbstractButton;
        protected var titleLabel:JLabel;
        protected var icon:Icon;
        protected var text:String;
        protected var titleEnabled:Boolean;
        protected var minimizeHeight:int;
        protected var buttonPane:Container;
        protected var buttonPaneLayout:SoftBoxLayout;
        protected var owner:JWindow;
        protected var frame:JFrame;

        public function JFrameTitleBar(){
            this.titleEnabled = true;
            this.minimizeHeight = 22;
            setLayout(new FrameTitleBarLayout());
            this.buttonPane = new Container();
            this.buttonPane.setCachePreferSizes(false);
            this.buttonPaneLayout = new SoftBoxLayout(SoftBoxLayout.X_AXIS, 0);
            this.buttonPane.setLayout(this.buttonPaneLayout);
            var _local1:Container = new Container();
            _local1.setBorder(new EmptyBorder(null, new Insets(-3)));
            _local1.setLayout(new BorderLayout());
            this.titleLabel = new JLabel();
            this.titleLabel.setHorizontalAlignment(JLabel.LEFT);
            this.titleLabel.setVerticalAlignment(JLabel.TOP);
            this.titleLabel.setUIElement(true);
            _local1.setUIElement(true);
            _local1.append(this.titleLabel, BorderLayout.CENTER);
            _local1.mouseEnabled = false;
            _local1.mouseChildren = false;
            append(_local1, BorderLayout.CENTER);
            var _local2:Container = new Container();
            _local2.setLayout(new BorderLayout());
            _local2.append(this.buttonPane, BorderLayout.NORTH);
            append(_local2, BorderLayout.EAST);
            this.setIconifiedButton(this.createIconifiedButton());
            this.setMaximizeButton(this.createMaximizeButton());
            this.setRestoreButton(this.createRestoreButton());
            this.setCloseButton(this.createCloseButton());
            this.setMaximizeButtonVisible(false);
            this.buttonPane.appendAll(this.iconifiedButton, this.restoreButton, this.maximizeButton, this.closeButton);
            this.buttonPane.setUIElement(true);
            this.updateUI();
        }
        override public function updateUI():void{
            setUI(UIManager.getUI(this));
        }
        override public function getDefaultBasicUIClass():Class{
            return (BasicFrameTitleBarUI);
        }
        protected function createPureButton():JButton{
            var _local1:JButton = new JButton();
            _local1.setBackgroundDecorator(null);
            _local1.setMargin(new Insets());
            _local1.setStyleTune(null);
            _local1.setBackground(null);
            _local1.setForeground(null);
            _local1.setMideground(null);
            _local1.setStyleProxy(this);
            return (_local1);
        }
        protected function createIconifiedButton():AbstractButton{
            return (this.createPureButton());
        }
        protected function createMaximizeButton():AbstractButton{
            return (this.createPureButton());
        }
        protected function createRestoreButton():AbstractButton{
            return (this.createPureButton());
        }
        protected function createCloseButton():AbstractButton{
            return (this.createPureButton());
        }
        public function setButtonIconGap(_arg1:int):void{
            this.buttonPaneLayout.setGap(_arg1);
        }
        public function getSelf():Component{
            return (this);
        }
        public function setFrame(_arg1:JWindow):void{
            if (this.owner){
                this.owner.removeEventListener(FrameEvent.FRAME_ABILITY_CHANGED, this.__frameAbilityChanged);
                this.owner.removeEventListener(InteractiveEvent.STATE_CHANGED, this.__stateChanged);
                this.owner.removeEventListener(WindowEvent.WINDOW_ACTIVATED, this.__activeChange);
                this.owner.removeEventListener(WindowEvent.WINDOW_DEACTIVATED, this.__activeChange);
            };
            this.owner = _arg1;
            this.frame = (_arg1 as JFrame);
            if (this.owner){
                this.owner.addEventListener(FrameEvent.FRAME_ABILITY_CHANGED, this.__frameAbilityChanged, false, 0, true);
                this.owner.addEventListener(InteractiveEvent.STATE_CHANGED, this.__stateChanged, false, 0, true);
                this.owner.addEventListener(WindowEvent.WINDOW_ACTIVATED, this.__activeChange, false, 0, true);
                this.owner.addEventListener(WindowEvent.WINDOW_DEACTIVATED, this.__activeChange, false, 0, true);
            };
            this.__stateChanged(null);
            repaint();
        }
        public function getFrame():JWindow{
            return (this.owner);
        }
        public function setTitleEnabled(_arg1:Boolean):void{
            this.titleEnabled = _arg1;
        }
        public function isTitleEnabled():Boolean{
            return (this.titleEnabled);
        }
        public function setMinimizeHeight(_arg1:int):void{
            this.minimizeHeight = _arg1;
        }
        public function getMinimizeHeight():int{
            return (this.minimizeHeight);
        }
        public function addExtraControl(_arg1:Component, _arg2:int):void{
            if (_arg2 == AsWingConstants.LEFT){
                this.buttonPane.insert(0, _arg1);
            } else {
                this.buttonPane.append(_arg1);
            };
        }
        public function removeExtraControl(_arg1:Component):Component{
            return (this.buttonPane.remove(_arg1));
        }
        public function getLabel():JLabel{
            return (this.titleLabel);
        }
        public function setIcon(_arg1:Icon):void{
            this.icon = _arg1;
            if (this.titleLabel){
                this.titleLabel.setIcon(_arg1);
            };
        }
        public function getIcon():Icon{
            return (this.icon);
        }
        public function setText(_arg1:String):void{
            this.text = _arg1;
            if (this.titleLabel){
                this.titleLabel.setText(_arg1);
            };
        }
        public function getText():String{
            return (this.text);
        }
        public function isActive():Boolean{
            if (this.owner){
                return (this.owner.isActive());
            };
            return (true);
        }
        public function setIconifiedButton(_arg1:AbstractButton):void{
            var _local2:int;
            if (this.iconifiedButton != _arg1){
                _local2 = -1;
                if (this.iconifiedButton){
                    _local2 = this.buttonPane.getIndex(this.iconifiedButton);
                    this.buttonPane.removeAt(_local2);
                    this.iconifiedButton.removeActionListener(this.__iconifiedPressed);
                };
                this.iconifiedButton = _arg1;
                if (this.iconifiedButton){
                    this.buttonPane.insert(_local2, this.iconifiedButton);
                    this.iconifiedButton.addActionListener(this.__iconifiedPressed);
                };
            };
        }
        public function setMaximizeButton(_arg1:AbstractButton):void{
            var _local2:int;
            if (this.maximizeButton != _arg1){
                _local2 = -1;
                if (this.maximizeButton){
                    _local2 = this.buttonPane.getIndex(this.maximizeButton);
                    this.buttonPane.removeAt(_local2);
                    this.maximizeButton.removeActionListener(this.__maximizePressed);
                };
                this.maximizeButton = _arg1;
                if (this.maximizeButton){
                    this.buttonPane.insert(_local2, this.maximizeButton);
                    this.maximizeButton.addActionListener(this.__maximizePressed);
                };
            };
        }
        public function setRestoreButton(_arg1:AbstractButton):void{
            var _local2:int;
            if (this.restoreButton != _arg1){
                _local2 = -1;
                if (this.restoreButton){
                    _local2 = this.buttonPane.getIndex(this.restoreButton);
                    this.buttonPane.removeAt(_local2);
                    this.restoreButton.removeActionListener(this.__restorePressed);
                };
                this.restoreButton = _arg1;
                if (this.restoreButton){
                    this.buttonPane.insert(_local2, this.restoreButton);
                    this.restoreButton.addActionListener(this.__restorePressed);
                };
            };
        }
        public function setCloseButton(_arg1:AbstractButton):void{
            var _local2:int;
            if (this.closeButton != _arg1){
                _local2 = -1;
                if (this.closeButton){
                    _local2 = this.buttonPane.getIndex(this.closeButton);
                    this.buttonPane.removeAt(_local2);
                    this.closeButton.removeActionListener(this.__closePressed);
                };
                this.closeButton = _arg1;
                if (this.closeButton){
                    this.buttonPane.insert(_local2, this.closeButton);
                    this.closeButton.addActionListener(this.__closePressed);
                };
            };
        }
        public function getIconifiedButton():AbstractButton{
            return (this.iconifiedButton);
        }
        public function getMaximizeButton():AbstractButton{
            return (this.maximizeButton);
        }
        public function getRestoreButton():AbstractButton{
            return (this.restoreButton);
        }
        public function getCloseButton():AbstractButton{
            return (this.closeButton);
        }
        public function setIconifiedButtonVisible(_arg1:Boolean):void{
            if (this.getIconifiedButton()){
                this.getIconifiedButton().setVisible(_arg1);
            };
        }
        public function setMaximizeButtonVisible(_arg1:Boolean):void{
            if (this.getMaximizeButton()){
                this.getMaximizeButton().setVisible(_arg1);
            };
        }
        public function setRestoreButtonVisible(_arg1:Boolean):void{
            if (this.getRestoreButton()){
                this.getRestoreButton().setVisible(_arg1);
            };
        }
        public function setCloseButtonVisible(_arg1:Boolean):void{
            if (this.getCloseButton()){
                this.getCloseButton().setVisible(_arg1);
            };
        }
        private function __iconifiedPressed(_arg1:Event):void{
            if (((this.frame) && (this.isTitleEnabled()))){
                this.frame.setState(JFrame.ICONIFIED, false);
            };
        }
        private function __maximizePressed(_arg1:Event):void{
            if (((this.frame) && (this.isTitleEnabled()))){
                this.frame.setState(JFrame.MAXIMIZED, false);
            };
        }
        private function __restorePressed(_arg1:Event):void{
            if (((this.frame) && (this.isTitleEnabled()))){
                this.frame.setState(JFrame.NORMAL, false);
            };
        }
        private function __closePressed(_arg1:Event):void{
            if (((this.frame) && (this.isTitleEnabled()))){
                this.frame.closeReleased();
            };
        }
        private function __activeChange(_arg1:Event):void{
            repaint();
        }
        private function __frameAbilityChanged(_arg1:FrameEvent):void{
            this.__stateChanged(null);
        }
        private function __stateChanged(_arg1:InteractiveEvent):void{
            if (this.frame == null){
                return;
            };
            var _local2:int = this.frame.getState();
            if (((((((((!((_local2 == JFrame.ICONIFIED))) && (!((_local2 == JFrame.NORMAL))))) && (!((_local2 == JFrame.MAXIMIZED_HORIZ))))) && (!((_local2 == JFrame.MAXIMIZED_VERT))))) && (!((_local2 == JFrame.MAXIMIZED))))){
                _local2 = JFrame.NORMAL;
            };
            if (_local2 == JFrame.ICONIFIED){
                this.setIconifiedButtonVisible(false);
                this.setMaximizeButtonVisible(false);
                this.setRestoreButtonVisible(true);
                this.setCloseButtonVisible(this.frame.isClosable());
            } else {
                if (_local2 == JFrame.NORMAL){
                    this.setIconifiedButtonVisible(this.frame.isResizable());
                    this.setRestoreButtonVisible(false);
                    this.setMaximizeButtonVisible(this.frame.isResizable());
                    this.setCloseButtonVisible(this.frame.isClosable());
                } else {
                    this.setIconifiedButtonVisible(this.frame.isResizable());
                    this.setRestoreButtonVisible(this.frame.isResizable());
                    this.setMaximizeButtonVisible(false);
                    this.setCloseButtonVisible(this.frame.isClosable());
                };
            };
            revalidateIfNecessary();
        }

    }
}//package org.aswing 
